package com.winchain.provider.expiration.service.impl;

import com.shell.bee.base.utils.StringUtil;
import com.winchain.chainbill.bill.base.po.CbDfrl;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.service.CbDfrlService;
import com.winchain.chainbill.bill.base.service.CbRecService;
import com.winchain.provider.expiration.service.BillExpQueryService;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service("billExpQueryService")
public class BillExpQueryServiceImpl implements BillExpQueryService {
	@Autowired
	private CbRecService cbRecService;
	@Autowired
	private CbDfrlService cbDfrlService;

	public List<CbRec> selectExpChdBills(String expDate, String grpId) {
		return selectExpChdBills(expDate, null, grpId);
	}

	private List<CbRec> selectExpChdBills(String expDate, String rootId, String grpId) {
		Example example = new Example(CbRec.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("expDate", expDate);
		criteria.andIsNotNull("prntId");
		criteria.andNotEqualTo("prntId", "");
		if (!StringUtil.emptyAndNull(rootId)) {
			criteria.andEqualTo("rootCode", rootId);
		}
		criteria.andEqualTo("grpId", grpId);
		List<Object> values = new ArrayList();

		values.add("S000");
		values.add("S004");

		values.add("S018");
		values.add("S027");
		values.add("S030");
		values.add("S031");

		criteria.andNotIn("billState", values);
		List<CbRec> bills = this.cbRecService.selectByExample(example);
		return bills;
	}

	public List<CbRec> selectExpSndBills(String expDate, boolean pm, String grpId) {
		List<String> notInStates = new ArrayList();
		if (!pm) {
			notInStates.add("S003");
			notInStates.add("S022");
			notInStates.add("S023");
			notInStates.add("S004");
			notInStates.add("S007");

			notInStates.add("S018");
			notInStates.add("S027");
			notInStates.add("S030");
			notInStates.add("S031");
		} else {
			notInStates.add("S004");

			notInStates.add("S018");
			notInStates.add("S027");
			notInStates.add("S030");
			notInStates.add("S031");
		}
		return this.cbRecService.selectExpSndBills(expDate, grpId, notInStates);
	}

	public List<CbRec> selectExpDlyBills(String expDate, String grpId) {
		List<String> notInStates = new ArrayList();

		notInStates.add("S004");

		notInStates.add("S018");
		notInStates.add("S027");
		notInStates.add("S030");
		notInStates.add("S031");
		return this.cbRecService.selectExpDlyBills(expDate, grpId, notInStates);
	}

	public Collection<CbRec> getExpChdBills(String expDate, String billId) {
		List<String> notInStates = new ArrayList();
		notInStates.add("S000");
		notInStates.add("S004");
		notInStates.add("S018");
		notInStates.add("S027");
		notInStates.add("S030");
		notInStates.add("S031");

		return this.cbRecService.selectExpChdBills(expDate, billId, notInStates);
	}

	public List<CbDfrl> selectExpDlyInsBills(String expDate, String grpId, String dfrlState) {
		List<String> notInStates = new ArrayList();
		notInStates.add("S027");
		notInStates.add("S030");
		notInStates.add("S031");
		return this.cbDfrlService.selectExpDlyInsBills(expDate, grpId, dfrlState, notInStates);
	}
}
